/*
	Author: Richard McKenna
			Stony Brook University
			Computer Science Department

	WRKeyEventHandler.cpp

	See WRKeyEventHandler.h for a class description.
*/

#include "stdafx.h"
#include "Cursor.h"
#include "WRKeyEventHandler.h"
#include "Game.h"
#include "GameInput.h"
#include "GameGraphics.h"
#include "GameGUI.h"
#include "GameTimer.h"
#include "StringTable.h"
#include "WindowsGameTimer.h"
#include "AnimatedSprite.h"
#include "SpriteManager.h"
#include "GamePhysics.h"

const int A_KEY = 0X41;
const int B_KEY = 0X42;
const int C_KEY = 0X43;
const int D_KEY = 0X44;

//const int X_KEY = 0X

void WRKeyEventHandler::handleKeyEvents(Game *game)
{
	wchar_t *title;
	wchar_t *counterText;
	GameInput *input = game->getInput();
	GameGraphics *graphics = game->getGraphics();
	
	if (game->getGameState() == GAME_PLAYING_GAME_STATE)
	{
		unsigned int aKey = (unsigned int)'A';
		unsigned int dKey = (unsigned int)'D';
		unsigned int sKey = (unsigned int)'S';
		unsigned int wKey = (unsigned int)'W';
		
		int incX = 0;
		int moveViewPortX = 0;
		int incY = 0;		
		int viewPortWidth = 200;
		int viewPortHeight = 200;
		bool moveViewport = false;		
		bool movePlayer = false;
		int playerState;

		GameWorld *world = game->getWorld();
		SpriteManager *spriteManager = world->getSpriteManager();
		AnimatedSprite *player = spriteManager->getPlayer();		

		if (input->isKeyDown(VK_ESCAPE))
		{
			game->changePreviousGameState(game->getGameState());
			game->changeGameState(GAME_PAUSE_STATE);
			return;
		}
		else if (input->isKeyDown(aKey) || input->isKeyDown(VK_LEFT))
		{
			if(player->getPhysicalProperties()->getX() > TILE_WIDTH)
			{
				incX = -5;
				if(player->getPhysicalProperties()->getX()==63*TILE_WIDTH-2 ||
					player->getPhysicalProperties()->getX()==31*TILE_WIDTH+1 ||
					player->getPhysicalProperties()->getX()==47*TILE_WIDTH-3 ||					
					player->getPhysicalProperties()->getX()==15*TILE_WIDTH)    								
				{
					moveViewPortX = -16*TILE_WIDTH;
					moveViewport = true;
				}
			}
			if(player->getCurrentState() != RUNNING_LEFT)
			{
				playerState = RUNNING_LEFT;
				player->setCurrentState(playerState);
			}
			movePlayer = true;
		}
		else if (input->isKeyDown(dKey) || input->isKeyDown(VK_RIGHT))
		{
			if(player->getPhysicalProperties()->getX() < WORLD_WIDTH-2*TILE_WIDTH)
			{
				incX = 5;
				if(player->getPhysicalProperties()->getX()==63*TILE_WIDTH-2 ||
					player->getPhysicalProperties()->getX()==31*TILE_WIDTH+1 ||
					player->getPhysicalProperties()->getX()==47*TILE_WIDTH-3 ||					
					player->getPhysicalProperties()->getX()==15*TILE_WIDTH)    								
				{
					moveViewPortX = 16*TILE_WIDTH;
					moveViewport = true;	
				}				
			}
			if(player->getCurrentState() != RUNNING_RIGHT)
			{
				playerState = RUNNING_RIGHT;
				player->setCurrentState(playerState);
			}
			movePlayer = true;
			
		}
		else if (input->isKeyDown(VK_RETURN))
		{

			int playerColumn1	= game->getPhysics()->column(player->getPhysicalProperties()->getX()+1);
			int playerColumn2	= game->getPhysics()->column(player->getPhysicalProperties()->getX()+TILE_WIDTH-1);
			int playerRow1		= game->getPhysics()->row(player->getPhysicalProperties()->getY()+1);
			int playerRow2		= game->getPhysics()->row(player->getPhysicalProperties()->getY()+TILE_HEIGHT-1);				

			int aiID = game->getPhysics()->killIt(playerRow1, playerColumn1, playerRow2, playerColumn2, game);
			if(aiID >= 0)
			{				
				//game->getAI()->getBot(aiID)->getAnimatedSprite()->setCurrentState(4);
				// successfully kill it				
			}

			if(player->getCurrentState() == STANDING_RIGHT || 
				(player->getCurrentState() == RUNNING_RIGHT) ||
				(player->getCurrentState() == ATTACKING_RIGHT) ||
				(player->getCurrentState() == JUMPING_RIGHT))
			{
				if(player->getCurrentState() != ATTACKING_RIGHT)
				{
					playerState = ATTACKING_RIGHT;
					player->setCurrentState(playerState);
				}			
			}
			else
			{
				if(player->getCurrentState() != ATTACKING_LEFT)
				{
					playerState = ATTACKING_LEFT;
					player->setCurrentState(playerState);
				}
			}
		}				
		else if(player->getCurrentState() == ATTACKING_RIGHT || (player->getCurrentState() == RUNNING_RIGHT))
		{
			playerState = STANDING_RIGHT;
			player->setCurrentState(playerState);
		}
		else if(player->getCurrentState() == ATTACKING_LEFT || (player->getCurrentState() == RUNNING_LEFT))
		{
			playerState = STANDING_LEFT;
			player->setCurrentState(playerState);
		}
		if (input->isKeyDown(VK_SPACE))
		{			
			incY = -10;
			if(player->getCurrentState() == STANDING_RIGHT || 
				(player->getCurrentState() == RUNNING_RIGHT) ||
				(player->getCurrentState() == ATTACKING_RIGHT) ||
				(player->getCurrentState() == JUMPING_RIGHT))
			{
					playerState = JUMPING_RIGHT;
					player->setCurrentState(playerState);							
			}
			else
			{
					playerState = JUMPING_LEFT;
					player->setCurrentState(playerState);
			}
			if(player->getPhysicalProperties()->getY() > TILE_HEIGHT*2
					&& player->getPhysicalProperties()->getY() < WORLD_HEIGHT-2*TILE_HEIGHT)		
				player->getPhysicalProperties()->incY(incY);
		}

		if(movePlayer)
		{	
			player->getPhysicalProperties()->incX(incX);			
		}

		if (moveViewport)
		{
			GameWorld *world = game->getWorld();
			Viewport *viewport = world->getViewport();
			viewport->moveViewport( moveViewPortX,
									0,
									world->getWorldWidth(),
									world->getWorldHeight());
		}



	}

	// 0X43 is HEX FOR THE 'C' VIRTUAL KEY
	// THIS CHANGES THE CURSOR IMAGE
	if ((input->isKeyDownForFirstTime(C_KEY))
		&& input->isKeyDown(VK_SHIFT))
	{
		Cursor *cursor = game->getGUI()->getCursor();
		StringTable *guiStringTable = graphics->getGUITextureManager()->getStringTable();
		int greenCursorID = guiStringTable->getIndexOfStringFromStringTable(L"textures/gui/cursor/white_cursor.bmp");
		int redCursorID = guiStringTable->getIndexOfStringFromStringTable(L"textures/gui/cursor/black_cursor.bmp");
		int currentCursorID = cursor->getActiveCursorID();
		if (currentCursorID == greenCursorID)
			cursor->setActiveCursorID(redCursorID);
		else
			cursor->setActiveCursorID(greenCursorID);
	}

	if (input->isKeyDown(VK_HOME))
	{
		WindowsGameTimer *timer = (WindowsGameTimer*)game->getTimer();
		int fps = timer->getTargetFPS();
		if (fps < 100)
			timer->setTargetFPS(fps + 1);
	}
	else if (input->isKeyDown(VK_END))
	{
		WindowsGameTimer *timer = (WindowsGameTimer*)game->getTimer();
		int fps = timer->getTargetFPS();
		if (fps > 1)
			timer->setTargetFPS(fps - 1);
	}
}